﻿var BangoStorage = function () {
	var pub = {};
	
	pub.ProfilePersistent = function (bangoUserId, date) {

		this.BangoUserId;
		this.Date;

		this.Date = date;
		this.BangoUserId = bangoUserId;
	};
	

	var _bangoStore;
	var _profilePersistent;
	var _keyName = "BangoStore";
	var _profilePersistentKey = "ProfilePersistent";
	var _profileId = -1;

	pub.getBatch = function () {
		return this.getProfile(_profileId);
	};

	pub.bangoStore = function () {
		return _bangoStore;
	};

	pub.ProfilePersistentForStore = function() {
		return _profilePersistent;
	};

	pub.profileId = function () {
		return _profileId;
	};

	pub.initStorage = function (useOfflineStorage) {
		var savedData;
		var savedProfilePersistent;
		if (useOfflineStorage) {
			try {
				//retreiving data
				savedData = localStorage.getItem(_keyName);
				savedProfilePersistent = localStorage.getItem(_profilePersistentKey);
				if ((savedData) && (savedData != null) && (savedData != undefined)) {
					_bangoStore = JSON.parse(savedData);
				}
				if ((savedProfilePersistent) && (savedProfilePersistent != null) && (savedProfilePersistent != undefined)) {
					_profilePersistent = JSON.parse(savedProfilePersistent);
				}
			}
			catch (ex) {
			}
		}

		if (_bangoStore == null) {
			_bangoStore = new Array();
		}
		if (_profilePersistent == null) {
			_profilePersistent = new Array();
		}
	};

	pub.saveStore = function (useOfflineStorage) {
		var dataToSave;
		if (useOfflineStorage) {
			try {
				dataToSave = JSON.stringify(_bangoStore);
				// Save to localStorage
				localStorage.setItem(_keyName, dataToSave);
			}
			catch (ex) {

			}
		}

	};
	
	pub.LogProfilePersistent = function (useOfflineStorage, bangoUserId) {
		var profilePersistentToSave;
		
		if (useOfflineStorage) {
			try {
				var savedProfilePersistent = this.GetProfilePersistent(useOfflineStorage);
				if (savedProfilePersistent.length > 0) {
					if (savedProfilePersistent[0].BangoUserId != bangoUserId) {
						localStorage.removeItem(_profilePersistentKey);
						_profilePersistent = [];
					}
				}
				_profilePersistent.push(new this.ProfilePersistent(bangoUserId, new Date()));

				profilePersistentToSave = JSON.stringify(_profilePersistent);
				localStorage.setItem(_profilePersistentKey, profilePersistentToSave);
				this.updateBangoUserId(useOfflineStorage, bangoUserId);

			}
			catch (ex){
				
			}
		}
	};
	
	pub.GetProfilePersistent = function (useOfflineStorage) {
		
		var savedProfilePersistent;
		if (useOfflineStorage) {
			savedProfilePersistent = localStorage.getItem(_profilePersistentKey);
			if ((savedProfilePersistent) && (savedProfilePersistent != null) && (savedProfilePersistent != undefined)) {
				_profilePersistent = JSON.parse(savedProfilePersistent);
			}
		}
		return _profilePersistent;
	};

	
	pub.getProfile = function (profileId) {
		var profile = null;

		if (_bangoStore != null) {
			for (var profileCounter = 0; profileCounter < _bangoStore.length; profileCounter++) {
				if (profileId == _bangoStore[profileCounter].profileId) {
					profile = _bangoStore[profileCounter];
					break;
				}
			}
		}
		return profile;
	};

	pub.getCurrentProfileIdFromStore = function () {
		var currentProfileId = 0;
		if (_bangoStore.length > 0) {
			for (var storedProfileIndex = 0; storedProfileIndex < _bangoStore.length; storedProfileIndex++) {
				if (_bangoStore[storedProfileIndex].profileId > currentProfileId) {
					currentProfileId = _bangoStore[storedProfileIndex].profileId;
				} 
			}
		}
		return currentProfileId;
	};

	pub.logProfile = function (useOfflineStorage, sdkType, sdkVersion, applicationId, applicationType, applicationChannel, applicationVersion, userId, bangoUserId, sessionRestartTime, connectionType, isRoaming, locationSource, locationAltitude, locationLatitude, locationLongitude, locationAccuracy, operatorCode, operatorName, countryISO, simOperator, deviceBrand, deviceMake, deviceModel, product, deviceVersion, operatingSystem, operatingSystemVersion, operatorConnectionType) {
		var profileId = -1;

		if (applicationId == null) {
			applicationId = "";
		}

		if (applicationType == null) {
			applicationType = "";
		}

		if (applicationChannel == null) {
			applicationChannel = "";
		}

		if (applicationVersion == null) {
			applicationVersion = "";
		}

		if (userId == null) {
			userId = "";
		}
		
		if (locationSource == null) {
			locationSource = "";
		}

		if (operatorCode == null) {
			operatorCode = "";
		}

		if (operatorName == null) {
			operatorName = "";
		}

		if (countryISO == null) {
			countryISO = "";
		}

		if (simOperator == null) {
			simOperator = "";
		}

		if (deviceBrand == null) {
			deviceBrand = "";
		}

		if (deviceMake == null) {
			deviceMake = "";
		}

		if (deviceModel == null) {
			deviceModel = "";
		}

		if (product == null) {
			product = "";
		}

		if (deviceVersion == null) {
			deviceVersion = "";
		}

		if (operatingSystem == null) {
			operatingSystem = "";
		}

		if (operatingSystemVersion == null) {
			operatingSystemVersion = "";
		}

		profileId = this.getCurrentProfileIdFromStore();

		profileId++;
		_bangoStore.push(new BangoProfile(profileId, sdkType, sdkVersion, applicationId, applicationType, applicationChannel, applicationVersion, userId, bangoUserId, sessionRestartTime, connectionType, isRoaming, locationSource, locationAltitude, locationLatitude, locationLongitude, locationAccuracy, operatorCode, operatorName, countryISO, simOperator, deviceBrand, deviceMake, deviceModel, product, deviceVersion, operatingSystem, operatingSystemVersion, operatorConnectionType));
		_profileId = profileId;
		this.saveStore(useOfflineStorage);
	};



	pub.logEvent = function (useOfflineStorage, connectionType, operatorConnectionType, errorLevel, eventName, eventValue, eventDetail, errorId, eventTime, eventLocalTime, eventParameters) {
		var profile = null;
		var eventId = -1;

		if (eventName == null) {
			eventName = "";
		}

		if (eventDetail == null) {
			eventDetail = "";
		}

		if (eventValue == null) {
			eventValue = "";
		}

		if (errorId == null) {
			errorId = "";
		}

		profile = this.getProfile(_profileId);
		if (profile != null) {
			eventId = profile.Events.length + 1;
			profile.Events.push(new BangoEvent(_profileId, eventId, connectionType, operatorConnectionType, errorLevel, eventName, eventValue, eventDetail, errorId, eventTime, eventLocalTime, eventParameters));
			this.saveStore(useOfflineStorage);
		}
		return eventId;
	};

	pub.updateLocation = function (useOfflineStorage, latitude, longitude, altitude, accuracy) {
		var profile = null;
		profile = this.getProfile(_profileId);
		if (profile != null) {
			profile.LocationX = longitude; //locationLongitude;
			profile.LocationY = latitude; //locationLatitude;
			profile.LocationZ = altitude; //locationAltitude;
			profile.LocationAccuracy = accuracy;
			this.saveStore(useOfflineStorage);
		}
	};

	pub.updateBangoUserId = function (useOfflineStorage, bangoUserId) {
		var profile = this.getProfile(_profileId);
		if (profile != null) {
			profile.BangoUserId = bangoUserId;
			this.saveStore(useOfflineStorage);
		}

	};

	pub.clearIsPostingFlag = function (useOfflineStorage, profileForPost) {
		for (eventCounter = 0; eventCounter < profileForPost.Events.length; eventCounter++) {
			profileForPost.Events[eventCounter].EventIsPosting = false;
		}
		this.saveStore(useOfflineStorage);

	};

	pub.updateEventsPosted = function (useOfflineStorage, profileForPost) {
		for (var eventCounter = 0; eventCounter < profileForPost.Events.length; eventCounter++) {
			profileForPost.Events[eventCounter].EventIsPosting = false;
			profileForPost.Events[eventCounter].EventPosted = true;
		}
		this.saveStore(useOfflineStorage);
	};

	pub.deleteEventsPosted = function (useOfflineStorage, profileId) {
		var returnValue = -1;
		if (_bangoStore != null && profileId != _profileId) {
			for (var profileCounter = 0; profileCounter < _bangoStore.length; profileCounter++) {
				if (profileId == _bangoStore[profileCounter].profileId) {
					_bangoStore.splice(profileCounter, 1);
					this.saveStore(useOfflineStorage);
					break;
				}
			}
		}
	};

	return pub;
}();
